<?php
namespace Libs\Common;
use Libs\Common\LogCommon;
/*
 * Class Axes Payment
 * @author ThamLT
 */
class AxesPaymentCommon {
	/**
	 * Generate hidden fields of to submit to axes page
	 * @author ThamLT
	 * */
	public static function generateHiddenField($aryData) {
		$sReturn = '';
		if (!empty($aryData)) {
			$sReturn .= '<input type="hidden" name="clientip" value="' . AXES_CLIENTIP . '" />';
			foreach ($aryData as $k => $v) {
				$sReturn .= '<input type="hidden" name="' . $k . '" value="' . $v . '" />';
			}
		}
		return $sReturn;
	}

	/**
	 * Generate url to send to axes page
	 * @author ThamLT
	 * */
	public static function generateUrl($aryData) {
		$sReturn = '';
		if (!empty($aryData)) {
			$sReturn .= 'clientip=' . urlencode(AXES_CLIENTIP);
			foreach ($aryData as $k => $v) {
				$sReturn .= '&' . $k . '=' . urlencode($v);
			}
		}
		return $sReturn;
	}

	/**
	 * Generate and re-Generate sendid to send/receive to axes payment
	 * @param $str : facebook id if isGen = true, fbid + time if isGen = false
	 *        $isGen: true, generate fbid + time with maximum 25 characeter to send to axes
	 *                false, re-generate fbId + time to get fbId received from axes
	 *        $maxLenght: maximum character that axes allow to send axes
	 * @return string
	 * @author ThamLT
	 * */
	public static function generateSendId($str, $isGen = true, $maxLength = 25) {
		$sReturn = '';
		if ($isGen) {
			$lenFb = strlen($str);
			$sublend = $maxLength - $lenFb - 1;
			// - 1 because include "_"
			$sReturn = substr(md5(time()), 0, $sublend) . '_' . $str;
		} else {
			$aryExplode = explode('_', $str);
			if (!empty($aryExplode[1])) {
				$sReturn = $aryExplode[1];
			}
		}
		return $sReturn;
	}

	/**
	 * Generate and re-Generate sendpoint
	 * @param $id: isGen = true: id of billing plan or purchase point or authetication point
	 *             isGen = false : string
	 *        $type = isGen = true, type is constant defined, false it's empty
	 *        $isGen: true/false
	 * @return string if isGen = true, array with key is id, type if isGen = false
	 * @author ThamLT
	 * */
	public static function generateSendPoint($id, $type = '', $isGen = true) {
		if ($isGen) {
			return $type . '_' . $id;
		} else {
			$aryExplode = explode('_', $id);
			if (!empty($aryExplode) && count($aryExplode) == 2) {
				return array('type' => $aryExplode[0], 'id' => $aryExplode[1]);
			}
		}
		return false;
	}

	/**
	 * Process when user leave
	 * @param array with 2 keys is telno & sendid
	 * @return int: 1: sucess
	 *              2: failure
	 *              3: maintenance
	 *              0: dont have any respone
	 * @author ThamLT
	 * */
	public static function leaveUser($aryData) {
		$iReturn = 0;
		if (!empty($aryData)) {
			$aryData['clientip'] = AXES_CLIENTIP;
			$aryData['sendiddel'] = 'yes';
			$fields_string = '';
			foreach ($aryData as $key => $value) {
				$fields_string .= $key . '=' . $value . '&';
			}
			rtrim($fields_string, '&');

			$ch = curl_init();
			//set the url, number of POST vars, POST data
			curl_setopt($ch, CURLOPT_URL, AXES_URL_BILLING_CONTINUE);
			curl_setopt($ch, CURLOPT_POST, count($aryData));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$result = curl_exec($ch);
			$result = trim($result);
			curl_close($ch);

			if (!empty($result)) {
				if ($result == 'SuccessOK') {
					$iReturn = 1;
				} elseif ($result == 'failure_order') {
					$iReturn = 2;
				} elseif ($result == 'maintenance') {
					$iReturn = 3;
				}
			}
		}
		return $iReturn;
	}

	/**
	 * Continue billing when last payment expired
	 * @author ThamLT
	 * */
	public static function continueBilling($aryData) {
		try {
			$aryData['clientip'] = AXES_CLIENTIP;
			$aryData['cardnumber'] = '8888888888888888';
			$aryData['send'] = 'cardsv';
			$aryData['expyy'] = '00';
			$aryData['expmm'] = '00';
			$aryData['printord'] = 'yes';
			$aryData['pubsec'] = 'yes';
			$fields_string = '';
			foreach ($aryData as $key => $value) {
				$fields_string .= $key . '=' . $value . '&';
			}
			rtrim($fields_string, '&');

			$ch = curl_init();
			//set the url, number of POST vars, POST data
			curl_setopt($ch, CURLOPT_URL, AXES_URL_BILLING_CONTINUE);
			curl_setopt($ch, CURLOPT_POST, count($aryData));
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$result = curl_exec($ch);
			//$result = trim($result);
			//curl_close($ch);

			if (!empty($result)) {
				return (string)$result;
			}
			return false;
		} catch (\Exception $ex) {
			$aryDataLog = array('AxesPaymentCommon.php at function continueBilling', $ex -> getMessage());
			LogCommon::write($aryDataLog);
			return false;
		}

	}

}
